/*
 * Created by JFormDesigner on Sat Jun 01 12:15:37 CST 2024
 */

package sy9_1;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

/**
 * @author ASUS
 */
public class NotePadeFram extends JFrame {
    public NotePadeFram() {
        initComponents();
    }

    private void openFile(ActionEvent e) {
        this.textArea.setText("");
        JFileChooser fileChooser = new JFileChooser();
        if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
            File selectedFile = fileChooser.getSelectedFile();
            this.setTitle("我的记事本"+selectedFile.getAbsolutePath());

            FileReader fr = null;
            try {
                fr = new FileReader(selectedFile);
                char[] cbuf = new char[1024];
                int len = -1;
                while ((len = fr.read(cbuf)) != -1) {
                    String s = new String(cbuf, 0, len);
                    System.out.print(s);
                    this.textArea.append(s);
                }
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }finally {
                if (fr != null){
                    try {
                        fr.close();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            }

        };

    }

    private void newFile(ActionEvent e) {
        this.textArea.setText("");
    }

    private void saveFile(ActionEvent e) {
        JFileChooser fileChooser = new JFileChooser();
        if(fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION){
            File selectedFile = fileChooser.getSelectedFile();

            FileWriter fw = null;
            BufferedWriter bw = null;
            try {
                fw = new FileWriter(selectedFile);
                bw = new BufferedWriter(fw);
                String text = this.textArea.getText();
                bw.write(text);
            } catch (IOException ex) {
                ex.printStackTrace();
            }finally {
                if (bw != null){
                    try {
                        bw.close();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }if (fw != null){
                    try {
                        fw.close();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            }

        };


    }

    private void foundHander(ActionEvent e) {
        Color fondcolor = JColorChooser.showDialog(null, "设置字体颜色", Color.black);
        this.textArea.setForeground(fondcolor);
    }

    private void backcolor(ActionEvent e) {
        Color backcolor = JColorChooser.showDialog(null, "设置背景颜色", Color.black);
        this.textArea.setBackground(backcolor);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Open Source Project license - 淘宝星河飘雪
        menuBar1 = new JMenuBar();
        menu8 = new JMenu();
        openMenuItem1 = new JMenuItem();
        newMenuItem = new JMenuItem();
        saveMenuItem = new JMenuItem();
        menu9 = new JMenu();
        foundMenuItem = new JMenuItem();
        backMenuItem = new JMenuItem();
        scrollPane1 = new JScrollPane();
        textArea = new JTextArea();

        //======== this ========
        setTitle("\u6211\u7684\u8bb0\u4e8b\u672c");
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== menuBar1 ========
        {

            //======== menu8 ========
            {
                menu8.setText("\u6587\u4ef6");

                //---- openMenuItem1 ----
                openMenuItem1.setText("\u6253\u5f00");
                openMenuItem1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, KeyEvent.CTRL_DOWN_MASK));
                openMenuItem1.addActionListener(e -> openFile(e));
                menu8.add(openMenuItem1);

                //---- newMenuItem ----
                newMenuItem.setText("\u65b0\u5efa");
                newMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, KeyEvent.CTRL_DOWN_MASK));
                newMenuItem.addActionListener(e -> newFile(e));
                menu8.add(newMenuItem);

                //---- saveMenuItem ----
                saveMenuItem.setText("\u4fdd\u5b58");
                saveMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_DOWN_MASK));
                saveMenuItem.addActionListener(e -> saveFile(e));
                menu8.add(saveMenuItem);
            }
            menuBar1.add(menu8);

            //======== menu9 ========
            {
                menu9.setText("\u7f16\u8f91");

                //---- foundMenuItem ----
                foundMenuItem.setText("\u8bbe\u7f6e\u5b57\u4f53\u989c\u8272");
                foundMenuItem.addActionListener(e -> foundHander(e));
                menu9.add(foundMenuItem);

                //---- backMenuItem ----
                backMenuItem.setText("\u8bbe\u7f6e\u80cc\u666f\u989c\u8272");
                backMenuItem.addActionListener(e -> backcolor(e));
                menu9.add(backMenuItem);
            }
            menuBar1.add(menu9);
        }
        setJMenuBar(menuBar1);

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(textArea);
        }
        contentPane.add(scrollPane1);
        scrollPane1.setBounds(0, 10, 760, 490);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(760, 540);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Open Source Project license - 淘宝星河飘雪
    private JMenuBar menuBar1;
    private JMenu menu8;
    private JMenuItem openMenuItem1;
    private JMenuItem newMenuItem;
    private JMenuItem saveMenuItem;
    private JMenu menu9;
    private JMenuItem foundMenuItem;
    private JMenuItem backMenuItem;
    private JScrollPane scrollPane1;
    private JTextArea textArea;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
